var vm = new Vue({
    el:"#app",
    data(){
        return {
            //初始化
            UserNo:'',
            UserName:'',
            CarNoList:[],
            //搜索条件
            CarNo:'',
            ServiceStartDate:'',
            ServiceEndDate:'',
            // //监听
            // MaterialMoney:'',
            // ManhourMoney:'',

            modal1: false, //添加加油记录弹框
            formItem: {
                CreateManNo: '',
                CreateManName: '',
                CarNo: '',
                ServiceDate: '',
                ServiceMan: '',
                ServiceFactory: '',
                ServiceInfo: '',
                MaterialMoney: '',
                ManhourMoney: '',
                TotalMoney: '',
                Remark: ''
            },
            editmodal1:false,//编辑加油记录弹框
            editformItem: {
                ID:'',
                CreateManNo: '',
                CreateManName: '',
                CarNo: '',
                ServiceDate: '',
                ServiceMan: '',
                ServiceFactory: '',
                ServiceInfo: '',
                MaterialMoney: '',
                ManhourMoney: '',
                TotalMoney: '',
                Remark: ''
            },
            //搜索结果
            columns1: [
                {
                    type: 'index',
                    title: '序号',
                    width:46,
                    align: 'center',
                },
                {
                    title: '维修车辆',
                    key: 'CarNo',
                    align: 'center',
                },
                {
                    title: '维修时间',
                    key: 'ServiceDate',
                    align: 'center',
                },
                {
                    title: '修车人',
                    key: 'ServiceMan',
                    align: 'center',
                },
                {
                    title: '修理厂',
                    key: 'ServiceFactory',
                    align: 'center',
                },
                {
                    title: '材料费明细',
                    key: 'ServiceInfo',
                    align: 'center',
                    width:250
                },
                {
                    title: '材料费合计',
                    key: 'MaterialMoney',
                    align: 'center',
                },
                {
                    title: '工时费合计',
                    key: 'ManhourMoney',
                    align: 'center',
                },
                {
                    title: '合计',
                    key: 'TotalMoney',
                    align: 'center',
                },
                {
                    title: '备注',
                    key: 'Remark',
                    align: 'center',
                },
                {
                    title: '录入人',
                    key: 'CreateMan',
                    align: 'center',
                },
                {   title: '操作',
                    key: 'action',
                    align: 'center',
                    width:150,
                    render: (h, params) => {
                        return h('div', [
                            h('a', {
                                style: {
                                    marginRight: '5px',
                                    border : '0',
                                    fontSize: '14px',
                                },
                                on: {
                                    click: () => {
                                        var self = this;
                                        self.editformItem.ID = params.row.ID;
                                        self.editformItem.CreateManNo = self.UserNo;
                                        self.editformItem.CreateManName = self.UserName;
                                        self.editformItem.CarNo = params.row.CarNo;
                                        self.editformItem.ServiceDate = params.row.ServiceDate;
                                        self.editformItem.ServiceMan = params.row.ServiceMan;
                                        self.editformItem.ServiceFactory = params.row.ServiceFactory;
                                        self.editformItem.ServiceInfo = params.row.ServiceInfo;
                                        self.editformItem.MaterialMoney = params.row.MaterialMoney;
                                        self.editformItem.ManhourMoney = params.row.ManhourMoney;
                                        self.editformItem.TotalMoney = params.row.TotalMoney;
                                        self.editformItem.Remark = params.row.Remark;
                                        this.openeditmodel();
                                    }
                                }
                            },'编辑'),
                            h('span', {
                            },'|'),
                            h('a', {
                                style: {
                                    marginLeft: '5px',
                                    border : '0',
                                    fontSize: '14px',
                                },
                                on: {
                                    click: () => {
                                        var self = this;
                                        var ID = params.row.ID;
                                        self.confirm(ID);
                                    }
                                }
                            },'删除')
                        ])
                    }
                }
            ],
            data1: [],
        }
    },
    created(){
        this.initThisPage();
        this.queryAllCarNo();
    },
    // watch:{
    //     MaterialMoney:function(val) {
    //         this.formItem.MaterialMoney = val;
    //         this.formItem.TotalMoney = this.formItem.MaterialMoney*1 + this.formItem.ManhourMoney*1;
    //     },
    //     ManhourMoney:function (val) {
    //         this.formItem.ManhourMoney = val;
    //         this.formItem.TotalMoney = this.formItem.MaterialMoney*1 + this.formItem.ManhourMoney*1;
    //     }
    // },
    methods:{
        ok () {
        },
        cancel () {
        },
        initThisPage(){
            var self = this;
            $.ajax({
                url: "./goPCDD/initPage", //请求的url地址
                xhrFields: {
                    withCredentials: true
                },
                dataType: "json", //返回格式为json
                type: "GET", //请求方式
                processData: false, //对表单data数据是否进行序列化
                contentType: false, //dataType设置你收到服务器数据的格式
                headers: {
                    'Content-Type': 'application/json'
                },
                beforeSend: function() {
                    //请求前的处理
                },
                success: function(data) {
                    if(data.status==0){
                        // top.location.href = "../../login.html";
                        window.location.href="../../login.html";
                    }else{
                        self.UserNo = data.userno;
                        self.UserName = data.username;
                    }
                },
                complete: function() {
                    //请求完成的处理
                },
                error: function() {}
            });
        },
        queryAllCarNo(){
            var self = this;
            $.ajax({
                url: "./goPCDD/queryAllCarNo", //请求的url地址
                dataType: "json", //返回格式为json
                async:false,
                type: "GET", //请求方式
                processData: false, //对表单data数据是否进行序列化
                contentType: false, //dataType设置你收到服务器数据的格式
                headers: {
                    'Content-Type': 'application/json'
                },
                success: function(data) {
                    var carnojson = new Array();
                    var json = JSON.parse(data.data);
                    var tempcarnojson = json.data;

                    for(var i=0;i<tempcarnojson.length;i++){
                        var temp = {
                            value: tempcarnojson[i].CarNo,
                            label: tempcarnojson[i].CarNo
                        };
                        carnojson.push(temp)
                    }
                    self.CarNoList = carnojson;
                },
                error: function() {},
            });
        },
        //打开添加加油记录弹框
        openmodal1(){
            this.formItem.CreateManName = this.UserName;
            this.formItem.CreateManNo = this.UserNo;
            this.modal1 = true;
        },
        //打开编辑加油记录弹框
        openeditmodel(){
            var self = this;
            self.editmodal1 = true;
        },
        //保存维修记录
        saveService(){
            var self = this;
            var CreateManNo = self.formItem.CreateManNo;
            var CreateManName = self.formItem.CreateManName;
            var CarNo = self.formItem.CarNo;
            if(CarNo==null||CarNo==""){
                toastr.warning("请选择车牌号");
                return false;
            }
            var ServiceDate = self.formItem.ServiceDate;
            if(ServiceDate==null||ServiceDate==""){
                toastr.warning("请选择维修日期");
                return false;
            }
            var tempServiceDate = new Date(ServiceDate);
            ServiceDate = tempServiceDate.toLocaleDateString();


            var ServiceMan = self.formItem.ServiceMan;
            ServiceMan = ServiceMan.trim();
            if(ServiceMan==null||ServiceMan==""){
                toastr.warning("请录入修车人信息");
                return false;
            }
            var ServiceFactory = self.formItem.ServiceFactory;
            if(ServiceFactory==null||ServiceFactory==""){
                toastr.warning("请录入修车厂");
                return false;
            }

            var ServiceInfo = self.formItem.ServiceInfo;
            if(ServiceInfo==null||ServiceInfo==""){
                toastr.warning("请录入材料费明细");
                return false;
            }

            var MaterialMoney = self.formItem.MaterialMoney;
            if(MaterialMoney==null||MaterialMoney==""){
                toastr.warning("请录入材料费合计");
                return false;
            }else{
                if(self.isMianJi(MaterialMoney)){

                }else{
                    toastr.warning("材料费合计请录入两位小数");
                    return false;
                }
            }

            var ManhourMoney = self.formItem.ManhourMoney;
            if(ManhourMoney==null||ManhourMoney==""){
                toastr.warning("请录入工时费合计");
                return false;
            }else{
                if(self.isMianJi(ManhourMoney)){

                }else{
                    toastr.warning("工时费合计请录入两位小数");
                    return false;
                }
            }
            var TotalMoney = self.formItem.TotalMoney;
            var Remark = self.formItem.Remark;

            var jsondata = {
                "CreateMan":CreateManName,
                "CarNo":CarNo,
                "ServiceDate":ServiceDate,
                "ServiceMan":ServiceMan,
                "ServiceFactory":ServiceFactory,
                "ServiceInfo":ServiceInfo,
                "MaterialMoney":MaterialMoney,
                "ManhourMoney":ManhourMoney,
                "TotalMoney":TotalMoney,
                "Remark":Remark,
            }
            var data_json = JSON.stringify(jsondata);
            $.ajax({
                url: "./goPCDD/saveService", //请求的url地址
                xhrFields: {
                    withCredentials: true
                },
                dataType: "json", //返回格式为json
                data: data_json, //参数值
                type: "POST", //请求方式
                headers : {
                    'Content-Type' : 'application/json'
                },
                beforeSend: function() {
                    //请求前的处理
                },
                success: function(data) {
                    if(data.status=='1'){
                        toastr.success('保存成功');
                        self.formItem.CreateManNo = "";
                        self.formItem.CreateManName = "";
                        self.formItem.CarNo = "";

                        self.formItem.ServiceDate = "";
                        self.formItem.ServiceMan = "";
                        self.formItem.ServiceFactory = "";
                        self.formItem.ServiceInfo = "";
                        self.formItem.MaterialMoney = "";
                        self.formItem.ManhourMoney = "";
                        self.formItem.TotalMoney = "";
                        self.formItem.Remark = "";
                        self.querytable();
                    }else{
                        toastr.error('保存失败，请重新录入');
                    }
                },
                complete: function() {
                    //请求完成的处理
                },
                error: function(data) {

                }
            });
        },
        //编辑维修记录
        editService(){
            var self = this;
            var ID = self.editformItem.ID;
            var CreateManNo = self.editformItem.CreateManNo;
            var CreateManName = self.editformItem.CreateManName;
            var CarNo = self.editformItem.CarNo;
            if(CarNo==null||CarNo==""){
                toastr.warning("请选择车牌号");
                return false;
            }
            var ServiceDate = self.editformItem.ServiceDate;
            if(ServiceDate==null||ServiceDate==""){
                toastr.warning("请选择维修日期");
                return false;
            }
            var tempServiceDate = new Date(ServiceDate);
            ServiceDate = tempServiceDate.toLocaleDateString();


            var ServiceMan = self.editformItem.ServiceMan;
            ServiceMan = ServiceMan.trim();
            if(ServiceMan==null||ServiceMan==""){
                toastr.warning("请录入修车人信息");
                return false;
            }
            var ServiceFactory = self.editformItem.ServiceFactory;
            if(ServiceFactory==null||ServiceFactory==""){
                toastr.warning("请录入修车厂");
                return false;
            }

            var ServiceInfo = self.editformItem.ServiceInfo;
            if(ServiceInfo==null||ServiceInfo==""){
                toastr.warning("请录入材料费明细");
                return false;
            }

            var MaterialMoney = self.editformItem.MaterialMoney;
            if(MaterialMoney==null||MaterialMoney==""){
                toastr.warning("请录入材料费合计");
                return false;
            }else{
                if(self.isMianJi(MaterialMoney)){

                }else{
                    toastr.warning("材料费合计请录入两位小数");
                    return false;
                }
            }

            var ManhourMoney = self.editformItem.ManhourMoney;
            if(ManhourMoney==null||ManhourMoney==""){
                toastr.warning("请录入工时费合计");
                return false;
            }else{
                if(self.isMianJi(ManhourMoney)){

                }else{
                    toastr.warning("工时费合计请录入两位小数");
                    return false;
                }
            }
            var TotalMoney = self.editformItem.TotalMoney;
            var Remark = self.editformItem.Remark;

            var jsondata = {
                "ID":ID,
                "CreateMan":CreateManName,
                "CarNo":CarNo,
                "ServiceDate":ServiceDate,
                "ServiceMan":ServiceMan,
                "ServiceFactory":ServiceFactory,
                "ServiceInfo":ServiceInfo,
                "MaterialMoney":MaterialMoney,
                "ManhourMoney":ManhourMoney,
                "TotalMoney":TotalMoney,
                "Remark":Remark,
            }
            var data_json = JSON.stringify(jsondata);
            $.ajax({
                url: "./goPCDD/editService", //请求的url地址
                xhrFields: {
                    withCredentials: true
                },
                dataType: "json", //返回格式为json
                data: data_json, //参数值
                type: "POST", //请求方式
                headers : {
                    'Content-Type' : 'application/json'
                },
                beforeSend: function() {
                    //请求前的处理
                },
                success: function(data) {
                    if(data.status=='1'){
                        toastr.success('编辑成功');
                        self.editformItem.CreateManNo = "";
                        self.editformItem.CreateManName = "";
                        self.editformItem.CarNo = "";

                        self.editformItem.ServiceDate = "";
                        self.editformItem.ServiceMan = "";
                        self.editformItem.ServiceFactory = "";
                        self.editformItem.ServiceInfo = "";
                        self.editformItem.MaterialMoney = "";
                        self.editformItem.ManhourMoney = "";
                        self.editformItem.TotalMoney = "";
                        self.editformItem.Remark = "";
                        self.querytable();
                    }else{
                        toastr.error('编辑失败，请重新录入');
                    }
                },
                complete: function() {
                    //请求完成的处理
                },
                error: function(data) {

                }
            });
        },
        querytable(){
            var self = this;
            var info ;
            var CarNo = self.CarNo;
            var ServiceStartDate = self.ServiceStartDate;
            if(ServiceStartDate==''||ServiceStartDate==null){
                ServiceStartDate = '';
            }else{
                var tempServiceStartDate = new Date(ServiceStartDate);
                ServiceStartDate = tempServiceStartDate.toLocaleDateString();
            }


            var ServiceEndDate = self.ServiceEndDate;
            if(ServiceEndDate==''||ServiceEndDate==null){
                ServiceEndDate = '';
            }else{
                var tempServiceEndDate = new Date(ServiceEndDate);
                ServiceEndDate = tempServiceEndDate.toLocaleDateString();
            }

            var jsondata={"CarNo":CarNo,"ServiceStartDate":ServiceStartDate,"ServiceEndDate":ServiceEndDate};
            var data_json = JSON.stringify(jsondata);
            $.ajax({
                url: "./goPCDD/queryAllService", //请求的url地址
                dataType: "json", //返回格式为json
                data: data_json, //参数值
                async:false,
                type: "POST", //请求方式
                processData: false, //对表单data数据是否进行序列化
                contentType: false, //dataType设置你收到服务器数据的格式
                headers: {
                    'Content-Type': 'application/json'
                },
                success: function(data) {
                    info = data.data;
                },
                error: function() {},
            });
            if(info){
                self.data1 = info;
            }
        },
        confirm (ID) {
            var self = this;
            this.$Modal.confirm({
                title: '提示',
                content: '<p>是否删除</p>',
                onOk: () => {
                    self.delService(ID);
                },
                onCancel: () => {
                    toastr.info("取消删除");
                }
            });
        },
        // 删除弹框
        delService(ID){
            var self = this;
            var jsondata = {
                "ID":ID,
            }
            var data_json = JSON.stringify(jsondata);
            $.ajax({
                url: "./goPCDD/delService", //请求的url地址
                xhrFields: {
                    withCredentials: true
                },
                dataType: "json", //返回格式为json
                data: data_json, //参数值
                type: "POST", //请求方式
                headers : {
                    'Content-Type' : 'application/json'
                },
                beforeSend: function() {
                    //请求前的处理
                },
                success: function(data) {
                    if(data.status=='1'){
                        toastr.success("删除成功");
                        self.querytable();
                    }else{
                        toastr.error('删除失败，请刷新页面后重试');
                    }
                },
                complete: function() {
                    //请求完成的处理
                },
                error: function(data) {

                }
            });
        },
        //导出excel
        inportexcel: function() {
            var self = this;
            var CarNo = self.CarNo;
            var ServiceStartDate = self.ServiceStartDate;
            if(ServiceStartDate==''||ServiceStartDate==null){
                ServiceStartDate = '';
            }else{
                var tempServiceStartDate = new Date(ServiceStartDate);
                ServiceStartDate = tempServiceStartDate.toLocaleDateString();
            }


            var ServiceEndDate = self.ServiceEndDate;
            if(ServiceEndDate==''||ServiceEndDate==null){
                ServiceEndDate = '';
            }else{
                var tempServiceEndDate = new Date(ServiceEndDate);
                ServiceEndDate = tempServiceEndDate.toLocaleDateString();
            }
            // var jsondata={"AssessDate":AssessDate,"Store": Store,"Post":Post,"SalerID":SalerID,"CID":CID};
            // var data_json = JSON.stringify(jsondata);
            window.location.href="./goPCDD/inportServiceExcel?CarNo="+CarNo+"&ServiceStartDate="+ServiceStartDate+"&ServiceEndDate="+ServiceEndDate;
        },
        //手机号验证
        isPoneAvailable(str) {
            var myreg=/^[1][3,4,5,7,8][0-9]{9}$/;
            if (!myreg.test(str)) {
                return false;
            } else {
                return true;
            }
        },
        //uuid
        uuid(len, radix) {
            var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
            var uuid = [], i;
            radix = radix || chars.length;

            if (len) {
                // Compact form
                for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random()*radix];
            } else {
                // rfc4122, version 4 form
                var r;

                // rfc4122 requires these characters
                uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
                uuid[14] = '4';

                // Fill in random data. At i==19 set the high bits of clock sequence as
                // per rfc4122, sec. 4.1.5
                for (i = 0; i < 36; i++) {
                    if (!uuid[i]) {
                        r = 0 | Math.random()*16;
                        uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
                    }
                }
            }

            return uuid.join('');
        },
        //两位小数验证
        isMianJi(mj){
            var reg = /^[1-9]+\d*(\.\d{0,2})?$|^0?\.\d{0,2}$/;
            if(reg.test(mj)){
                return true;
            }else{
                return false;
            }
        },
        //正整数验证
        isPositiveInteger(s){//是否为正整数
            var re = /^[0-9]+$/ ;
            return re.test(s)
        }
    }
})